package assignment1;

/*
 * @author Lachlan Collins JC198809
 * MainUi Class to alleviate need for static functions in Main Class
 */
import java.io.*;

public class MainUi {

    private String PRODUCTFILE = "productList";
    private String TABSPACE = "		";

    public void run() {
        //Make Linked List
        Product productList = new Product();

        readFile(productList, PRODUCTFILE);

        do {
            System.out.println("CP2004 JAVA JC198809");
            System.out.println("~Product Menu~");
            System.out.println("1: List Products");
            System.out.println("2: List Inventory");
            System.out.println("3: List Products with Inventory"); // FIX MENU
            System.out.println("0: Quit");
            System.out.print("Selection:");

            switch (getUserInput()) {
                case 1:
                    System.out.println(printProducts(productList));
                    break;
                case 2:
                    System.out.println(printProducts(productList));
                    System.out.print("Please Select a Product for Inventory:");
                    System.out.println(printInventory(getUserInput(), productList));
                    break;
                case 3:
                    System.out.println(printProductsItems(productList));
                    break;
                case 0:
                    writeFile(productList, "productList");
                    System.exit(0);
                    break;
                default:
                    break;
            }
        } while (true);
    }

    private int getUserInput() {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String userInput = null;
        try {
            userInput = br.readLine();
            return Integer.parseInt(userInput);
        } catch (IOException ioe) {
            System.out.println("IO error reading input");
            System.exit(1);
        }
        return 0;
    }

    private String printProducts(Product list) {
        String returnString = "";
        int count = 0;
        for (Product cursor = list; cursor != null; cursor = cursor.getNextProduct()) {
            count++;
            returnString += "#" + count + " Qty: " + cursor.getQty() + " - Name: " + cursor.getName() + "\n";
        }
        return returnString;
    }

    private String printProductsItems(Product list) {
        String returnString = "";
        for (Product cursor = list; cursor != null; cursor = cursor.getNextProduct()) {
            returnString += "Qty: " + cursor.getQty() + " - Name: " + cursor.getName() + "\n";
            if (cursor.getNextInventory() != null) {
                for (Inventory invntCursor = cursor.getNextInventory(); invntCursor.getNext() != null; invntCursor = invntCursor.getNext()) {
                    returnString += TABSPACE + "Qty: " + invntCursor.getQty() + " - Name: " + invntCursor.getName() + "\n";
                }
            }
        }
        return returnString;
    }

    private void readFile(Product list, String fileName) {
        try {
            FileReader fileReader = new FileReader(fileName + ".txt");
            BufferedReader reader = new BufferedReader(fileReader);
            String line = null;
            int count = 0;
            while ((line = reader.readLine()) != null) {

                String[] splitInventory = line.split(TABSPACE);
                if (splitInventory.length > 1) {

                    if (list.getNextInventory() == null) {
                        list.setNextInventory(new Inventory());
                    }
                    Inventory cursor = list.getNextInventory();
                    while (cursor.getNext() != null) {
                        cursor = cursor.getNext();
                    }
                    cursor.setNext(new Inventory());

                    String[] splitQty = splitInventory[1].split(" - ");
                    splitQty = splitQty[0].split("Qty: ");
                    cursor.setQty(Integer.parseInt(splitQty[1]));

                    String[] splitName = splitInventory[1].split(" - ");
                    splitName = splitName[1].split("Name: ");
                    cursor.setName(splitName[1]);

                } else {
                    if (list.getNextProduct() == null && count > 0) {
                        list.setNextProduct(new Product());
                        list = list.getNextProduct();
                    }
                    String[] splitQty = line.split(" - ");
                    splitQty = splitQty[0].split("Qty: ");
                    list.setQty(Integer.parseInt(splitQty[1]));

                    String[] splitName = line.split(" - ");
                    splitName = splitName[1].split("Name: ");
                    list.setName(splitName[1]);
                }
                count++;
            }
            reader.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void writeFile(Product list, String fileName) {
        try {
            FileWriter fileWriter = new FileWriter(fileName + ".txt");
            BufferedWriter writer = new BufferedWriter(fileWriter);
            writer.write(printProductsItems(list));
            writer.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private String printInventory(int productNumber, Product list) {
        String returnString = "\n" + "List of Inventory for: ";
        Product cursor = list;
        if (productNumber > 0) {
            for (int i = 0; i == productNumber; i++) {
                cursor = cursor.getNextProduct();
            }
            returnString += cursor.getName() + "\n";
            for (Inventory invntCursor = cursor.getNextInventory(); invntCursor.getNext() != null; invntCursor = invntCursor.getNext()) {
                returnString += "Qty: " + invntCursor.getQty() + " - Name: " + invntCursor.getName() + "\n";
            }
        }
        return returnString;
    }
}
